more efficient calculating of path