Fix resource leaks on read errors
Fix immediate variable reassignment
Merge pull request #307 from mscdex/master

misc fixes