Remove unused variables

Uncovered by clang's static analyzer
Avert nullptr deref

Uncovered by clang's static analysis
Avoid division (modulo) by zero

Uncovered by llvm's static analysis