Rewrite of Python code