Apply PR #1446 to safe_refactor branch.