refactor scala code