Add tests and adjust code for Sonar rules