Added some comments and refactoring