Modified main loop