refactored code again. made functions smaller