Added refactored version in C++