WIP fix for #786

Not ready for merging yet, but thought I'd link it up to the issue for any comments.