fix bugs in function