added subsample function