Backported more or less everything.