refine ex1.39 by better test case and refactoring