matrix bugfix
Many refactorings