Fix bugs caught by static analysis