Added checks to ensure that states actually exist before transitioning to them when reordering.