Backported #2178 to v1.2.x

Thanks @mkcor