refactor & bug fix
still need to clean up
need benchmark