Definite leaks using valgrind

Start testing for definite leaks via valgrind. Fix all the leaks it found, as well as leaks in tests.