These four lines are repeated below - maybe combine them in a function, say addPluginIfValid.