Updated scala solution