Logic refactored