fix bugs and add lib functions.