Refactor to reduce difference from upstream