code refine and bug fix