Solution to problem2