Changed separate if-blocks to if-elseif blocks; avoids wasteful computations.