Rewritten to python