Refactored main window event handlers