Tidy up event handling code.