Fixed memory leaks

Memory leaks found by valgrind.