refine diff function