Replaced locks with eventloop.

In this PR, all events and incoming messages are dispatched by the event loop.
This eliminates the locks, and ensures that no more than one event handler can be running at a time.