improve solution to problem 1 and add a test