refactored code to keep functions isolated