added the resampling code