Giant commit: switch to new sampling mechanisms, fix some of the initial bugs.
Warning: many things are probably still broken at this stage, and the tutorials/scripts
have not been updated to reflect the new API.