Refactored the foreach statement to push towards individual functions. This means we can test the functions in the future.