fixed some memory related bugs