Backport some stuff from the internal version