Refactoring the logic