Fix additional bugs found via static analysis