cleaned up event handling code