better c++ implementation