backported some commit from the original