Various EM code improvements by Charles Vaske and Andy Nguyen