Added debug printing and fixed bug