fixing distance calc - taking margin into account