Fix some resource leaks/memory leaks.