fixed resampling code