Fixed leaking file descriptors