Rewrite Claw code for parity with the original project, keeping behavior aligned across versions and changes