Refactoring und further finetuning (matrix is taking far too much memory)