Fixed algorithm bugs.