Rewrite loop unrolling

Now much faster and simpler.