Moved task management out of the event loop