Index: trunk/psLib/src/math/psStats.c
===================================================================
--- trunk/psLib/src/math/psStats.c	(revision 36237)
+++ trunk/psLib/src/math/psStats.c	(revision 36238)
@@ -1048,5 +1048,5 @@
 
 	
-#if (0)
+#if (CZW)
 	printf("CZW: bad sigma?: %f %f  %f %f  %f %f  %f %f %f  %f\n",
 	       binH2F32,binL2F32,binHiF32,binLoF32,binH4F32,binL4F32,
@@ -1289,6 +1289,8 @@
         // XXX can we calculate the binMin, binMax **before** building this histogram?
         // if the range is too absurd, adjust numBins & binSize
+	// We no longer want to reset the binSize here, as it can cause odd things.  Better to select
+	// a number of bins, and then set the min/max values to put those bins sanely around the mean.
         long numBins = PS_MAX (50, PS_MIN (10000, (max - min) / binSize));
-        binSize = (max - min) / (float) numBins;
+	//        binSize = (max - min) / (float) numBins;
         psTrace(TRACE, 6, "The new min/max values are (%f, %f).\n", min, max);
         psTrace(TRACE, 6, "The new bin size is %f.\n", binSize);
@@ -1296,4 +1298,12 @@
 
 
+#define FITTED_CLIPPING_NUM 5.0
+	if (min < guessMean - FITTED_CLIPPING_NUM * guessStdev) {
+	  min = guessMean - FITTED_CLIPPING_NUM * guessStdev;
+	}
+	if (max > guessMean + FITTED_CLIPPING_NUM * guessStdev) {
+	  max = guessMean + FITTED_CLIPPING_NUM * guessStdev;
+	}
+	
         psHistogram *histogram = psHistogramAlloc(min, max, numBins); // A new histogram (without outliers)
         if (!psVectorHistogram(histogram, myVector, errors, mask, maskVal)) {
@@ -1330,4 +1340,5 @@
         PS_BIN_FOR_VALUE (binMin, histogram->bounds, guessMean - minFitSigma*guessStdev, 0);
         PS_BIN_FOR_VALUE (binMax, histogram->bounds, guessMean + maxFitSigma*guessStdev, 0);
+
         if (binMin == binMax) {
             COUNT_WARNING(10, 100, "Failed to calculate the min/max of the input vector.\n");
@@ -1356,4 +1367,5 @@
         psTrace(TRACE, 6, "The clipped peak value is %f\n", histogram->nums->data.F32[binPeak]);
 
+	
 	float lowfitMean = NAN;
 	float lowfitStdev = NAN;
