Reworked error handling.

Added ErrorHandler class to deal with Xi runtime errors.