properly handle stream