Restructurisation: moved debug code into general routine, and into aux file