improve the logic