ported first half of Java example to Scala