Optimizations

Here's the optimized code, complete with updated tests. Note, however, that I did not add *new* tests, I merely made the old ones work with the change in the internal API.