Initial rewrite in Scala.
With large jumping point thanks to http://javatoscala.com