Refactored event listener