Refactoring to have a more scala-like code