Fix memory leaks

Basically a full refactor, with a slight API change.  Valgrind
is now reporting 0 bytes of lost memory, however :)