clean up c++ & re-use code