fixed some algorithm bugs.