extract event handling code to own method