fix some memory bugs