Scala rewrite and clean up. Part 3