fixed memory bugs