Rework moments routines for improved numerical stability based on