some bug fixed and refactor