Refactoring, fix some errors and add more test