move the event loop body into a function