fixed calcResiduals