rewriting in python