BF: fix code in format_matrix, update tests