Index: /branches/2dbias/psModules/src/detrend/pmOverscan.c
===================================================================
--- /branches/2dbias/psModules/src/detrend/pmOverscan.c	(revision 42716)
+++ /branches/2dbias/psModules/src/detrend/pmOverscan.c	(revision 42717)
@@ -441,6 +441,28 @@
 		if (yscan->col0 >= xscan->col0 && yscan->col0 + yscan->numCols <= xscan->col0 + xscan->numCols)
 		{
-			// define where to normalize the xReduced vector
-			int j0 = yscan->col0 - xscan->col0 + (int)(yscan->numCols / 2) - 1;
+			// define how to normalize the xReduced vector
+			// slice the part overlapping withe yoverscan from xReduced
+			long startIndex = yscan->col0 - xscan->col0;
+			long normSize = xReduced->n - startIndex; // Calculate the size of the new vector
+			// Allocate the new vector
+			psVector *normVector = psVectorAlloc(normSize, PS_TYPE_F32);
+			// Copy the data from xReduced starting from startIndex to the end
+			for (long i = startIndex; i < xReduced->n; i++)
+			{
+				normVector->data.F32[i - startIndex] = xReduced->data.F32[i];
+			}
+			// Now normVector holds the sliced data; then, compute its robust mean
+			psStatsOptions statistic = PS_STAT_CLIPPED_MEAN;
+			psStats *stats = psStatsAlloc(statistic);
+			if (!psVectorStats(stats, normVector, NULL, NULL, 0))
+			{
+				psError(PS_ERR_UNKNOWN, false, "failure to measure clipped mean as normalization of xReduced");
+				psFree(stats);
+				psFree(normVector);
+				return NULL;
+			}
+			float xReducedNormalized = psStatsGetValue(stats, statistic);
+			psFree(stats);
+			psFree(normVector);
 			// XXX apply the 2D bias correction here
 			int yDiff = yscan->row0 - image->row0; // y offset between the science and the yoverscan region
@@ -452,5 +474,5 @@
 					int iy = i + yDiff;
 					int jx = j + xDiff;
-					image->data.F32[i][j] -= yReduced->data.F32[iy] - xReduced->data.F32[j0] + xReduced->data.F32[jx];
+					image->data.F32[i][j] -= yReduced->data.F32[iy] - xReducedNormalized + xReduced->data.F32[jx];
 				}
 			}
