Code cleanup

Code cleanup to make Sonar happy, also more unit tests.