memory leak fixes courtesy of valgrind