Added debug writing (ugly though.)