Refactoring some codes and updating test codes