Plug leaks

Armed with my trusty valgrind, I fought against leaks and refcounts to clean up the library and its tests.

I ran with `-xonline` and fix every leak except for the one that we can still see in `test_index_collision__add` which I haven't quite managed to track down yet.