refactored and simplified plugin