Created logically working C++ version.