finally got the algorithm working