clear code and fix some bugs