Add some comments and optimization