Moved function to cpp