patches from @riban, see #466