main function
refactoring