add more comments and do some refactor