Avoid possible overruns of iterators

Avoid possible overruns of iterators.