Fixing a bug when transition leads to same position in matrix for some type of inputs