Various memory leaks fixes