Refactored one piece of code to use foreach instead of while.