Refactoring some for loops to use range-based for loops instead. No functional changes intended.