fix some bug, and refactoring