fix some memory and exit issues