iterative version with loop