modified main loop