Ver 1 of solution for problem 3