Static analysis

This is a first batch of things a static analysis tool found. These are failures to close files and streams.
I don't know - that looks broken to me too.