added C++ implementation