slighly improved code speed; worst case is now much better