Refactored to use pattern matching in the for comprehension
Removed conversion to Java list; refactored to use pattern matching in the for comprehension