Refactor for more idomatic Scala