fix bugs and add functions