Refactored matrix creation and probability calculation.