added debug code, fixed bug