Beautufy matrix building
Add exitflag