Rewrote in Scala

Will merge when finished. This pull request should be used to discuss the rewrite and show us what is going on and what is changing.