delete all the test code and fix some bugs as James's comments