I am not 100% sure this is correct, but if not correct, it is at least no more wrong than the old code!