clean properly and close channels on end of input stream