Don't close bad filedescriptors

* Add a check for fd > 0 before closing
* Removed close call when fd < 0