Reimplemented chap2 Java -> Scala