Add some comments and refactor.