Speed up implementation of B-test

The kernel functions are a little more verbose, but in my tests this is roughly an order of magnitude faster. cc @BigCrunsh