Correctly close open pipes after use

* Still needs testing