Fixes some bugs and makes debug message more informative