Refactor, add some comments