refine code logic