more fixes to the algorithm