Trying to fix the sampling function.