all codes re-writen in cpp