Refactor event handler