Reworked so that empty attributes are always removed.