Refactors stream code