Refactored logic on if statement