improved solution to problem 7
improved solution to problem 7