Add missed functions and fix some bugs.