Refactored implementation to use fewer Streams and Lambdas.