Refactoring some codes