Refactors some event-loop code into a Runnable class