Turns out that the previous linear way of checking was wrong, back to quadratic