Refactor error handlers