fix memory bugs