better upsert logic