fix bug and simplify