                     //printf("score for thread %d location %d is %d with prev_score %d\n", thread_idx, j, score, prev_score);
                 }
 
-                max_score = maxscore[0];
+                // Load current max score from shared memory.
+                max_score = maxscore[thread_idx % 32];
 
                 // While there are changes to the horizontal score values, keep updating the matrix.
                 // So loop will only run the number of time there are corrections in the matrix.


    start = clock64();

    //int16_t max_score = SHRT_MIN;
    //int16_t max_i = -1;
    //int16_t max_j = -1;
    int16_t max_score = maxscore[0];
    int16_t max_i = maxi[0];
    int16_t max_j = maxj[0];

    long long int serial = 0;


                {
                    uint32_t val_thread = (read_count - 1) % 32;
                    max_score = __shfl_sync(0xffffffff, score, val_thread);
                    maxscore[thread_idx % 32] = max_score;
                    maxi[thread_idx % 32] = __shfl_sync(0xffffffff, i, val_thread);
                    maxj[thread_idx % 32] = __shfl_sync(0xffffffff, j, val_thread);

                }
            }

