Rebased #3848

Rebased version of @pborreli's #3848