Optimizing script speed