implement residual functions