Fixed bug in main loop.