Merge most functionality into main plugin because it was getting far too complex.