bug fixes to sampling code