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: In LFT(DELTA,M) form with uncertain blocks at the top
[M,UB,BlkStruct] = ulftdata2(D);

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

if numel(bsel)<numel(UB)
   % Move uncertain blocks back to the bottom and update model
   UB = UB(bsel);
   [nc,nr] = iosize(UB);
   [rs,cs] = size(IC);
   D.IC = IC([nr+1:rs,1:nr],[nc+1:cs,1:nc]);
   D.Blocks = [M.Blocks ; UB];
end
