simplify main loop a bit