Fixed some memory bugs