Bug fixed and algorithm improved