Optimizing Python Script