fixed some potential file descriptor leaks