Static analysis

This is a first batch of things a static analysis tool found. These are failures to close files and streams.