improved pattern handling