mutualizing code to add result with pattern matching check
modification to ease toggling debug mode