python code, shared with andrew