added calculaciton of max residual