move stream out of while