fix couple of use-after-free issues