Consolidate event handling code in one function