Enhanced logic and refactorig