Fixed some bugs in the code