Converted while loops to for loops