fix bug, and refactor.