fixing bug on sample function