Ported from private repo