solved a bug and simplify a bit