Use while loop instead of iterator.foreach.