refactoring, and fixed unittest