Script to optimize patterns