Refactor out GetNextSelection; add command replace_next