fix some bug and refactor the code