Revert changes from #1416 and find a better fix

This reverts commit 9539afa2aee67d21f3d6a6d715ef97428910af11, reversing changes made to 541e65b71f0bf454e81945e9aece740be2103c7c. It adds a new commit to fix the issue, which I hope is more reliable.

cc @pgiraud