Middleware and upsert start