Cleanup of code and fix some warnings from static code analysis