Refactored to the event loop