refactor code to main routine