removed len(pat) method calls to possibly speed up the algorithm