optimized the algorithm for internal labels, and added some commentaries