Refactored event loop handling.
Further implementation.