replaced for loops with more idiomatic for ... range loops