Refactor event handling