Rewrite to more idiomatic scala (& simpler?)