Added some debug messages, as well as fix a potential bug.