Fixed upsert bug