Refactored PluginService.cs to reduce nesting