Added memory clean up, passes valgrind now