fix several bugs and add more lib functions.