fixed memory leaks found by valgrind