fix memory management bug