Resource fix

This fixes a couple of issues, most importantly unclosed resources.