refactor error handling.