Refactor and add some comments (ref #54)