bug fixed and code refactoring