Refactor Scala P1