backported changes from master