Fix and debug code