Refactoring main functions