refine code, fix bug of refine