bug fix and optimize code