Fixed some bugs with egm routines