finalized distance code