added gracefully failure and better debug printing