Fixing some algorithm bugs.