porting spiht to cpp: 1st iteration