Work on #1035
Implements #1016
Merge pull request #1050 from emmanuel-keller/master

Bug fix and improvement