add cleanup code when exception is encountered
Merge pull request #3 from colindickson/master

addendum to fix for issue #1