reworked sampling code