Refactor commands for plugins

Fixes #509