modify algorithm, it is more efficent now.