Lower-case tags and be more careful about undefined values.

  For consistancy we downcase each tag value, to avoid "TEST" and "test"
 showing up as two distinct tags.

  Also updated to avoid some possible NULL values.