Improve handling of file descriptors