[lib] clean up code and some improvement