C# improvements ported to Python