Fixed some resource leaks