function [Ktot,T,bmod]= Kbrick(btot,D,detJt,detra,R,R_)

Knnr=zeros(30);
bmod=zeros(6,24,8);
for ii=1:8
    b=zeros(6,30);
    
    b(:,:)=btot(:,:,ii);
    detJ=detJt(ii,1);
    kp=b'*D*b*detJ;
    Knnr=Knnr+kp;
    
    
end

Knnr=Knnr.*detra;

invR_=inv(R_);
for count_j=1:8
% Additional B matrix modification
    bmod(:,:,count_j)=btot(:,1:24,count_j)-btot(:,25:30,count_j)/Knnr(25:30,25:30)*Knnr(25:30,1:24);
    bmod(:,:,count_j)=invR_*bmod(:,:,count_j)*R;
end

Ktot=Knnr(1:24,1:24)-Knnr(1:24,25:30)/Knnr(25:30,25:30)*Knnr(25:30,1:24);
T=-inv(Knnr(25:30,25:30))*Knnr(25:30,1:24);

end