moved the intializations out of the while loop. that was stupid. now the algorithm is behaving as expected.