Added better dispatching for the event loop