Move debug code earlier in the script