compute residual error and cleanup unused code