BUG fixed
move all code to cpp