original cpp version (expanded)