Simplify some code in the event listener