Back port the changes from #5560