Rewrite to behave in an 'upsert' manner.

It should now update any existing entries and associated records, rather
than delete/recreate the identifiers/other names/etc. Also fix a number
of bugs.