Implemented a simpler scala solution