Better error handling, fixes #2