Scala solution