tidy up sampling code