C++ implementation
fix output