real final version, fix some bugs