Added tests for memory leaks and fixed a few leaks