Issue 2166

I think this should prevent the crash in Issue 2166, though it probably doesn't fix the root cause -- which I haven't been able to figure out. I also cleaned up some stuff I forgot to remove in the last commit.