refactoring of gibbs sampler