Fixing file handle leaks

Some more resource leak fixes. I did a bit of a quick job it seems, the first time round. Hopefully this resolves all of the resource leaks when handling exceptions.