Sort optimisation

Should help with #1195

I think the problem is that successive filters may take more or less time to sort and finish out of order.

I've fixed a big performance bottleneck, and added some more checks for newer filter operations.