fix memory errors