backported some fixes