Working solution to Problem6