Some algorithmic speedups

While using IPSet() some operations took rather longer than I liked. Looking at some of the code it seemed it could be done better. So here I have rewritten some of the core methods to be (on my machine anyway) about 3-5 times faster. Functionally there is no visible difference.