Fix residual order calculation