Factored out sampling functions, see #4