improved sampler code