Fixed a  bunch of memory leaks