fixed the wiki link
further refactoring of next_states func
further refactoring -much faster