Some refactoring, documentation and test cases