Update code structure and bug fix