fixed previous commit
refactored and fix a bug