fixed bugs and added function

fixed bugs and added function