Better error handling. Fixes #4