         }
         float magnitude = mag[i];
         magAvg[i] = magAvg[i] * 0.99 + magnitude * 0.01;
-        if (magnitude > maxMag) {
-            maxMag = magnitude;
+        if (magAvg[i] > maxMag && i > 0) {
+            maxMag = magAvg[i];
             maxIndex = i;
         }
 

}

float prevPhase[END_INDEX];
uint16_t _old_max_index = 0;
float phaseAvg[END_INDEX];
float phaseRateAverage[END_INDEX];
float magAvg[END_INDEX];

    //  digitalWrite(LED_PIN, HIGH);
    //}

    winningIndex = maxIndex;
    return ret;
}

