Switch to C++
Fix infinite loops