Added MAtrix and fixed some memory issues