simplify scala solution to 3