Rewrite handleStream to be easier to read.