Improve event processing logic