refactored some for loops