yet another sampling fix and refactoring