Refactored code in atlas to use foreach