Optimised colouring function, should now use less CPU.