working c++ implmentation