Iterative version

Not yet optimised