Refactoring, add a lot of comments