fixed bugs in reordering matrix