improved get_next_url function