move sampling functions into the main code