complete rewrite of ERI routine