rewrite the core to c++