fix memory leak. what is the advantage of dynamic allocation here?