Fix bug and refactor.