Global variable not sticking

If global variables are mysteriously changing value, start by looking for array bound overflows.

Example from your code snippet:

long positions[2];
. . .
positions[x] = xMotor.currentPosition() - sensorOffset[x]*stepsPerMM;

If subscript x has a value of 2 or more, the array positions[2] will over flow and corrupt other memory space, maybe causing the phenomenon you have experienced.