reorganized subsample function