Refactor event dispatching