Static analysis

Performed static analysis and fixed several of the reported issues.