some fixes in the algorithm

still slow,