Minor optimisation to extend, based on jquery's implementation