Numerous memory leakage fixes