Additional bug fixes and better debug messages