Refactored for more Scala style.