refine logic
merge newest code