Solution to prob 1 a bit nicer