adjustments to handle upserting empty records and clean them properly