Many static variables in this function are falsely labeled with a g prefix, as if it were global, when they are in fact state variables.