Fixing up some bugs I found in the code