fix memory leak; more informative error messages