refactoring MainWindow codes