refactoring and fix code warnings from PVS Studio