Rewrote fixTagAttributes() for robustness and correctness: