fixed a bug of custom patterns not found and simplifed code