Corrected distance and cleaned code