calculate_matrix_width should be more robust now