fixed the main loop