Fix memory leak

Kudos to LLVM scan-build static analyzer
Fix a division by zero

Kudos to the LLVM scan-build static analyzer