add test case and refine code