simplify main loop, moved EOF exception out.