Remove the event loop code into a separate object so that we can share the same loop for multiple nodes. Some cleanups