Refactored code to avoid recursive call for efficiency

Fixed deprecation warning for default_scope