Working implementation using streams and lambdas