Fixes for #962, needs testing