added residual function