Fixed (#841)

This is a fix for #841 and supporting test.
Should have waited for the reply from @khellang, got lost in woods of branches, and screwed it up. Can you guys make any sense of it?