Fix code and refactoring