python: improvements in alg