modified: rewriting to c++