Added EM code by Charlie Vaske (and cleaned up the style a bit)