Fixed main loop