Refactored MainFrame event handlers to reduce nesting