Contributed by Alessandro Andrioni. (cherry picked from commit 100e8a1ae1d930dde084af7d1281e491c7f124ec)