Refactor code for stream reading.