refactored Main.scala