Preliminary attempt at splitting commands out

Note this isn't quite working yet due to some weirdness in the code...