fix some memory leaks found with valgrind