first, iterative version