Add more comments and refactor