refactoring and code analysis fixes