Refactored some for loops to use map and filter