Backported from Thrall sources