Improve error reporting

This is a refactoring change.

Ref #102