Fixed some bugs and added some debugging options