Improve method of calculating modular mean