fixed the event handling code