Refactored matrix utilities