Change thread execution order, performance much better. Fixed some
algorithm errors.