Refactor error handling.