fix memory management