prioritize_states() now actually sorts by weight.
The new algorithm is also runs in linear time.