Refactored stream handler