refactored to use stored data for marginals