Rewrite to Scala