Fixed a couple of issues, added some debugging output