Some code update and refactoring