refactoring && modify end condition.