Improve speed of pattern recognition