Rewrite to C++, old C code remains in `c_version` branch