Refactored code to highlight the stream processing.