Added unit test for invalid commands, fixed the bugs it revealed