further refactoring of next_states func