add some logic + bug fix