refactor and bug fixed.