Rewriting in C for speed/memory.