Fix division-by-zero logic error