Changed error handling as suggested in pull request #3 and issue #7