Optimised but untested

First pass at optimised code, ahead of testing. Deoptimising to follow,
sadly, possibly.