porting the Groovy version to Scala