A couple of further optimizations, plus some benchmarking code.