Added comments and refactoring