fixing valgrind memory leaks