Adding debugging output to chain writing function.