Clean up the scaling code --
replace long if-then-else chain by calculations.