Restructure main loop to wait 500ms each iteration, unless an event occurs.