Adding original C++ code for reference