upsert refactored