Finish several functions and fix bugs