This version can deal with dead ends