Replaced loops over i with iterators