correcting my code considering sonar advices