Refactored: extracted tag replace method.