Fix file descriptor leaks