Refactor logic into Command class. Add tests.