Fix bugs in all functions