  
      static int compareByContigAndScore(const void *first, const void *second);      // qsort()-style compare routine
      static int compareByScore(const void *first, const void *second);               // qsort()-style compare routine
-@@ -81,6 +140,8 @@ struct PairedAlignmentResult {
+@@ -81,6 +144,8 @@ struct PairedAlignmentResult {
  	int score[NUM_READS_PER_PAIR];              // score of each end if matched
  
  	int mapq[NUM_READS_PER_PAIR];               // mapping quality of each end, encoded like a Phred score (but as an integer, not ASCII Phred + 33).

         qnameLen, read->getId(),
         flags,
         contigName,
@@ -1263,7 +1303,8 @@ SAMFormat::writeRead(
         fullLength, quality,
         aux != NULL ? "\t" : "", auxLen, aux != NULL ? aux : "",
         readGroupSeparator, readGroupString,

     unsigned scoreLimit = maxK + extraSearchDepth;
 
     //
@@ -350,6 +352,7 @@ IntersectingPairedEndAligner::align(
                 } else {
                     offset = readLen[whichRead] - seedLen - nextSeedToTest;
                 }
+				minHashTableHits[whichRead][dir] = min(minHashTableHits[whichRead][dir], nHits[dir]);
                 if (nHits[dir] < maxBigHits) {
                     totalHashTableHits[whichRead][dir] += nHits[dir];
                     if (doesGenomeIndexHave64BitLocations) {
@@ -756,6 +759,7 @@ IntersectingPairedEndAligner::align(
                                     (*nSecondaryResults)++;
 
                                 }
+								secondBestPairScore = bestPairScore;
                                 bestPairScore = pairScore;
                                 probabilityOfBestPair = pairProbability;
                                 bestResultGenomeLocation[readWithFewerHits] = candidate->readWithFewerHitsGenomeLocation + fewerEndGenomeLocationOffset;
@@ -771,6 +775,9 @@ IntersectingPairedEndAligner::align(
 
                                 isBestHit = true;
                             } else {
+								if(pairScore > secondBestPairScore) {
+									secondBestPairScore = pairScore;
+								}
                                 if (maxEditDistanceForSecondaryResults != -1 && (unsigned)maxEditDistanceForSecondaryResults >= pairScore - bestPairScore) {
                                     //
                                     // A secondary result to save.
@@ -852,9 +859,28 @@ doneScoring:
 #endif  // DEBUG
         }
     } else {
