Fix memory leaks

This fixes a few memory leaks I revealed using valgrind.