add functions and fix some bugs

not tested