Added a bunch of debug statements, and also a few bug fixes