Write iterative version with tests passing.