Changed for loops to use ranged iterators