Refactoring of the main function