finish the 9.6 exercise and refactor code