tweaked main loop