restructure: define mainController and move code accordingly