fully iterative version