Refactor event listener