I actually found my mistake. I should be initializing the iterators in iter function because that is what a for loop calls every time. I will leave this question to help others who might have a similar problem.