Fixed bugs in the state labeling function