Better error handling

Initial implementation for the #4 issue.