fixed a couple of bugs, one of which greatly improved the speed.