Add some comments and done some refactoring