Fixed broken pipe

Closed file descriptors, where necessary, to allow piping to work.