Fix some cases where file resources were not closed correctly, mostly when Exceptions occur during opening files