refactoring, remove the duplication and useless code