fixed some memory bugs