global refactoring and optimization