Generalization for any number of gaussians

More painful than I imagined.