Fix a number of issues reported by Sonar.