Refactor stream into a more contained form.