It would be good to have a comment describing why the if's are like this (i.e. to reduce the number of comparisons necessary).