                 Hscan[tx]=H[tx];
             }
             __syncthreads();
-            if ( 1 <= tx && tx < 32 )
-                Hscan[tx]+=Hscan[tx-1];
-            __syncthreads();
-            if ( 2 <= tx && tx < 32 )
-                Hscan[tx]+=Hscan[tx-2];
-            __syncthreads();
-            if ( 4 <= tx && tx < 32 )
-                Hscan[tx]+=Hscan[tx-4];
-            __syncthreads();
-            if ( 8 <= tx && tx < 32 )
-                Hscan[tx]+=Hscan[tx-8];
-            __syncthreads();
-            if ( 16 <= tx && tx < 32 )
-                Hscan[tx]+=Hscan[tx-16];
-            __syncthreads();
-            if(tx<31){
-                if(Hscan[tx+1] > medPos && Hscan[tx] < medPos){
-                    *retval=tx+1;
-                    *countAtMed=Hscan[tx];
-                }
-                else if(Hscan[tx]==medPos){
-                  if(Hscan[tx+1]>medPos){
-                      *retval=tx+1;
-                      *countAtMed=Hscan[tx];
-                  }
-                }
-            }
+            scanNeighbor(Hscan, 32,  1, tx);
+            __syncthreads();
+            scanNeighbor(Hscan, 32,  2, tx);
+            __syncthreads();
+            scanNeighbor(Hscan, 32,  4, tx);
+            __syncthreads();
+            scanNeighbor(Hscan, 32,  8, tx);
+            __syncthreads();
+            scanNeighbor(Hscan, 32, 16, tx);
+            __syncthreads();
+
+            findMedian(Hscan, 31, tx, retval, countAtMed, medPos);
          }
 
     __global__ void cuMedianFilterMultiBlock(PtrStepSzb src, PtrStepSzb  dest, PtrStepSzi histPar, PtrStepSzi coarseHistGrid,int r, int medPos_)

                histogramMedianPar8LookupOnly(HCoarse,HCoarseScan,medPos, &firstBin,&countAtMed);
                __syncthreads();

                if ( luc[firstBin] <= (j-r))
                {
                    histogramClear32(HFine[firstBin]);
                    for ( luc[firstBin] = j-r; luc[firstBin] < ::min(j+r+1,cols); luc[firstBin]++ ){
                        histogramAdd32(HFine[firstBin], hist+(luc[firstBin]*256+(firstBin<<5) ) );
                    }
                }
                else{
                    for ( ; luc[firstBin] < (j+r+1);luc[firstBin]++ ) {
                        histogramAddAndSub32(HFine[firstBin],
                        hist+(::min(luc[firstBin],cols_m_1)*256+(firstBin<<5) ),
                        hist+(::max(luc[firstBin]-2*r-1,0)*256+(firstBin<<5) ) );
                        __syncthreads();

                    }
                }
                __syncthreads();

                int leftOver=medPos-countAtMed;
                if(leftOver>=0){

                Hscan[tx]=H[tx];
            }
            __syncthreads();
            scanNeighbor(Hscan, 8, 1, tx);
            __syncthreads();
            scanNeighbor(Hscan, 8, 2, tx);
            __syncthreads();
            scanNeighbor(Hscan, 8, 4, tx);
            __syncthreads();

            findMedian(Hscan, 7, tx, retval, countAtMed, medPos);
        }

        __device__ void histogramMedianPar32LookupOnly(int* H,int* Hscan, const int medPos,int* retval, int* countAtMed){
