add original C++ version for reference