[#2212] [#2222] CID37580:

Also check to make sure outFd isn't zero. and moved some declarations