Rewritten from scratch. ~2-4x speedup in complex use cases.