Refactoring. Some bug fixed.