Fixes for #996, needs testing