modified: rewrite to c++