refine the logic