New iterative (non-recursive) version
Final version