Cleaned up memory leaks and fixed a test