Improved error handling. Fixes #2.