Moved the event loop into a class