some codes refactored