fixing bugs with sampling code and adding tests re github issue #11