Make c++ code more robust