improve direction algorithm