Refactor scope matching