Implement command and fix some bugs

Still a few bugs lurking, will fix tomorrow.