Refactor errorhandler