simplify and improve double-free check