Pattern recognition. Fast impl. Ver 2.