added factory def to Scala, and refactored test accordingly