finished code for problem 6