Scala implementation, avoiding mutable vars

What a mess ;-)