Fixed bug in upsert()