Fixed some memory errors with valgrind.