fetch: refactor fetch_refs into two functions