Index: /trunk/psphot/src/psphotSourceFits.c
===================================================================
--- /trunk/psphot/src/psphotSourceFits.c	(revision 7330)
+++ /trunk/psphot/src/psphotSourceFits.c	(revision 7331)
@@ -263,9 +263,7 @@
 
     psArray *modelSet = psArrayAlloc (source->blends->n + 1);
-    // DROP modelSet->n = 0;
     psArrayAdd (modelSet, 16, PSF);
     
     psArray *sourceSet = psArrayAlloc (source->blends->n + 1);
-    // DROP sourceSet->n = 0;
     psArrayAdd (sourceSet, 16, source);
 
@@ -279,7 +277,5 @@
 
 	// create the model and guess parameters for this blend
-	blend->modelPSF = pmModelAlloc (PSF->type);
-	pmModel *model = blend->modelPSF;
-
+	pmModel *model = pmModelAlloc (PSF->type);
 	for (int j = 0; j < model->params->n; j++) {
 	    model->params->data.F32[j] = PSF->params->data.F32[j];
@@ -296,4 +292,7 @@
 	psArrayAdd (modelSet, 16, model);
 	psArrayAdd (sourceSet, 16, blend);
+
+	// free to avoid double counting model 
+	psFree (model);
     }
 
@@ -309,28 +308,26 @@
     double chiTrend = psPolynomial1DEval (psf->ChiTrend, PSF->params->data.F32[1]);
     PSF->chisqNorm = PSF->chisq / chiTrend;
-    // PSF->chisqNorm = PSF->chisq;
 
     // evaluate the blend objects, subtract if good, free otherwise
     for (int i = 1; i < modelSet->n; i++) {
-	pmSource *src = sourceSet->data[i];
-	pmModel *model = modelSet->data[i];
+	pmSource *blend = sourceSet->data[i];
+	pmModel *model  = modelSet->data[i];
 
 	// correct model chisq for flux trend
 	chiTrend = psPolynomial1DEval (psf->ChiTrend, model->params->data.F32[1]);
 	model->chisqNorm = model->chisq / chiTrend;
-	// model->chisqNorm = model->chisq;
-
-	// if we skip this one, free the corresponding blend entry model
-	if (!psphotEvalPSF (src, model)) {
-	    pmSource *blend = source->blends->data[i - 1];
-	    psFree (blend->modelPSF);
-	    blend->modelPSF = NULL;
+
+	// if this one failed, skip it
+	if (!psphotEvalPSF (blend, model)) {
+	    psTrace ("psphot.blend", 5, "failed on blend %d of %d\n", i, modelSet->n);
 	    continue;
 	}
 
+	// otherwise, supply the resulting model to the corresponding blend
+	blend->modelPSF = psMemIncrRefCounter (model);
 	psTrace ("psphot.blend", 5, "fitted blend as PSF\n");
 	pmModelSub (source->pixels, source->mask, model, false, false);
-	src->mode |=  PM_SOURCE_MODE_SUBTRACTED;
-	src->mode &= ~PM_SOURCE_MODE_TEMPSUB;
+	blend->mode |=  PM_SOURCE_MODE_SUBTRACTED;
+	blend->mode &= ~PM_SOURCE_MODE_TEMPSUB;
     }
     psFree (modelSet);
@@ -339,4 +336,5 @@
     // evaluate the primary object
     if (!psphotEvalPSF (source, PSF)) {
+	psTrace ("psphot.blend", 5, "failed on blend 0 of %d\n", modelSet->n);
 	psFree (PSF);
 	return false;
