Cleaned up some leaks and pointer errors thanks to valgrind