improved stream handling