Fixed numerous memory leaks. Changed many of the test cases to use a canonical