Move duplicate code for advancing the iterator to a common function. #4277