Total rewrite into C++