change bitshift to div-by-2, trust the compiler to do arithmetic shift rather than logical shift, also moved the reset of scaleFactor to a more logical place