refactor event handler