Reorganized the event handlers