refactor command handling (incomplete, but functional with bugs)