refactoring for optimization
further optimization in belief.propagation()