add unique algorithm and fix some bugs.