updated code with cpp functions