Avoid infinite loop in iterator