Fix several issues
Added debug logging