Fix some double-frees/use-after-free

r? @frank-trampe
Okay. I'll deal with the gotos on my next crusade.