Fix unclosed resources

Here's a cut at a couple of commits to close the previously leaked resources.