fixed memory leaks found by valgrind
fixed memory leaks found by valgrind