code refactoring to remove repeted code and use pluginInterface. still needs code cleaning