Fix bugs and add several functions