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