Revise C++ code and fix bugs