Replaces while iterators with foreach call