Insert fixes

Fixes 2 crashes on repeated typing. Having trouble writing tests for these, because I can't quite figure out the conditions that create the bugs. But I have seen both of these in stack traces, so I've added some guards to bail out if we have bad data.