Backport to 2.3.x fix for #1577

It's just a backport of @ojii code