Backport refactor from 2.0