Port tests to Scala
Fix a couple of bugs