formatting and a refactoring main function