Fix bugs in code