Properly close streams

Sorry about the previous spam. I missed a few things. Hopefully everything is according to your standards.