Some work on the main loop.