Fix some bugs found by static code analysis.