Complete rewrite of my previous plugin