MainWindow refactoring and code cleanup