Correct release and fix small memory leaks