fix some bugs and some refactoring