Refactorings. Extracted event loop into events.py.