optimized version of matched