Improved the main loop.