Destroy the context before removing the doc ref

Add more debug too