refactor main and mainLoop in methods