Working sampling function.