refactoring logic