refactor some code from the main file