fix memory management issues