Refactoring, improve comments and test cases