Fix leaking file descriptors