Copied out part of the compat code to eliminate circular import