Move stream code to stream.js