added proper endpoint orientation calculation