refactor out the subsetting code