optimised matching loop