refactored main functions