Bug fixes for python and C++ versions