implemented automatic resampling