Fix crashes as reported by andrewkm