Make state sorting code work