Be much more careful about closing files created by pipe.

Previously, it was possible to leak file descriptors (particularly in error
conditions) and eventually run out of them (though you needed to be unlucky to
hit such a condition).
Kill trailing whitespace.