fixes  for memory and file descriptor leaks