fix a error and refactor code