Backport #7259 to stable5
Partially backport #7305 to stable5