Fixed memory leaks hopefully. Still need to run valgrind to verify.