Fix issues uncovered by static analysis