Refactor some code into a ranged for loop.