working optimized matrix