Fixes some bugs in the code