Various changes

* add caching
* move some code from Clojure to C
* other refactorings