refactor code in main