Fixed bugs in event handlers