Iterative implementation passing basic tests.