Fixed bug in event handling