refactoring and fix a bug