translated some into C for reference