Reworked the iteration algorithm, finally all test cases work now.