Issue #846 and part of #835