Refactoring to fix issue with for loops.