Refactor error reporting

Fixes #123