fix another leak and better check of return values