some code fix + refactoring