used valgrind to remove some memory leaks