refactor out dispatchEvent and processEvent