function D = simplify(D,LevelOverride,MaxLevel)
% Simplifies redundant uncertainty blocks.

%   Copyright 2003-2011 The MathWorks, Inc.

% Pull out uncertainty blocks and related block data.
% Note: Upper LFT with respect to uncertain blocks
[M,UB,BlkStruct] = ulftdata2(D);

% Simplify
[mIC,bsel] = rctutil.simplifyLFT(M.IC.Response,BlkStruct,...
   LevelOverride,MaxLevel);

if numel(bsel)<numel(UB)
   % Move uncertain blocks back to the bottom
   UB = UB(bsel);
   [nc,nr] = iosize(UB);
   [rs,cs,~] = size(mIC);
   mIC = mIC([nr+1:rs,1:nr],[nc+1:cs,1:nc],:);
   
   % Update model
   D.IC.Response = mIC;
   Delay = D.IC.Delay;
   Delay.Input = Delay.Input(1:cs);
   Delay.Output = Delay.Output(1:rs);
   Delay.IO = Delay.IO(1:rs,1:cs);
   D.IC.Delay = Delay;
   D.Blocks = [M.Blocks ; UB];
end