Factor out data fetching into separate function