properly handle overlapped patterns