Replaced while loops with standard fors