Improve memory leaks found by valgrind.