Resource leak fixes