Exception, synchronization and style improvements

As suggested by SonarQube.