Fix bugs in functions