fixed a small problem in the more efficient algo