Fixes for issue #536: refactored error handler.