Firts pass for #863
Fix for #861